package in.muton.edushare.wicket.converters.choicerenderers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.form.IChoiceRenderer;

import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Universities;

public class PrivacyRenderer implements IChoiceRenderer<String> {

	private static Log log = LogFactory.getLog(PrivacyRenderer.class);
	
	public Object getDisplayValue(String arg0) {
		return arg0;
	}

	public String getIdValue(String arg0, int arg1) {
		return arg0;
	}
		
	public List<String> getOptions(){
		return options;
	}
	
	public void setPrivacyOption(String option){
		log.debug("Option = "+option);
		log.debug("entity = "+entity);
		if(entity == 0){
			if(option.compareTo(univPrivacy[0]) == 0)
				this.option = Messages.PRIVACY_TYPE_PUBLIC;
			else if(option.compareTo(univPrivacy[1]) == 0)
				this.option = Messages.PRIVACY_TYPE_SHARED;
			else if(option.compareTo(univPrivacy[2]) == 0)
				this.option = Messages.PRIVACY_TYPE_PRIVATE;
		}else if(entity == 1){
			if(option.compareTo(deptPrivacy[0]) == 0)
				this.option = Messages.PRIVACY_TYPE_PUBLIC;
			else if(option.compareTo(deptPrivacy[1]) == 0)
				this.option = Messages.PRIVACY_TYPE_SHARED;
			else if(option.compareTo(deptPrivacy[2]) == 0)
				this.option = Messages.PRIVACY_TYPE_PRIVATE;
		}else if(entity == 2){
			if(option.compareTo(coursePrivacy[0]) == 0)
				this.option = Messages.PRIVACY_TYPE_PUBLIC;
			else if(option.compareTo(coursePrivacy[1]) == 0)
				this.option = Messages.PRIVACY_TYPE_SHARED;
			else if(option.compareTo(coursePrivacy[2]) == 0)
				this.option = Messages.PRIVACY_TYPE_PRIVATE;
		}else if(entity == 3){
			if(option.compareTo(groupPrivacy[0]) == 0)
				this.option = Messages.PRIVACY_TYPE_PUBLIC;
			else if(option.compareTo(groupPrivacy[1]) == 0)
				this.option = Messages.PRIVACY_TYPE_SHARED;
			else if(option.compareTo(groupPrivacy[2]) == 0)
				this.option = Messages.PRIVACY_TYPE_PRIVATE;
		}
	}
	
	public String getPrivacyOption(){
		if(entity == 0){
			if(this.option == Messages.PRIVACY_TYPE_PUBLIC)
				return univPrivacy[0];
			else if(this.option == Messages.PRIVACY_TYPE_SHARED)
				return univPrivacy[1];
			else if(this.option == Messages.PRIVACY_TYPE_PRIVATE)
				return univPrivacy[2];
		}else if(entity == 1){
			if(this.option == Messages.PRIVACY_TYPE_PUBLIC)
				return deptPrivacy[0];
			else if(this.option == Messages.PRIVACY_TYPE_SHARED)
				return deptPrivacy[1];
			else if(this.option == Messages.PRIVACY_TYPE_PRIVATE)
				return deptPrivacy[2];
		}else if(entity == 2){
			if(this.option == Messages.PRIVACY_TYPE_PUBLIC)
				return coursePrivacy[0];
			else if(this.option == Messages.PRIVACY_TYPE_SHARED)
				return coursePrivacy[1];
			else if(this.option == Messages.PRIVACY_TYPE_PRIVATE)
				return coursePrivacy[2];
		}else if(entity == 3){
			if(this.option == Messages.PRIVACY_TYPE_PUBLIC)
				return groupPrivacy[0];
			else if(this.option == Messages.PRIVACY_TYPE_SHARED)
				return groupPrivacy[1];
			else if(this.option == Messages.PRIVACY_TYPE_PRIVATE)
				return groupPrivacy[2];
		}
		return null;
	}
	
	public int getoption(){
		return option;
	}
	
	public PrivacyRenderer(Object o){
		if(o instanceof Universities){
			this.options = Arrays.asList(univPrivacy);
			entity = 0;
			option = Messages.PRIVACY_TYPE_PUBLIC;
		}else if(o instanceof Departments){
			this.options = Arrays.asList(deptPrivacy);
			entity = 1;
			option = Messages.PRIVACY_TYPE_PUBLIC;
		}else if(o instanceof CourseInstances){
			this.options = Arrays.asList(coursePrivacy);
			entity = 2;
			option = Messages.PRIVACY_TYPE_PUBLIC;
		}else if(o instanceof Groups){
			this.options = Arrays.asList(groupPrivacy);
			entity = 3;
			option = Messages.PRIVACY_TYPE_PUBLIC;
		}else{
			options = new ArrayList<String>();
		}
	}
	
	private static String [] univPrivacy = {
		"Public", "Teachers and Administrators", "University Administrators"
	};
	
	private static String [] deptPrivacy = {
		"Public", "Teachers and Administrators", "Department Administrators"
	};
	
	private static String [] coursePrivacy = {
		"Public", "Teachers and Teaching Assistants", "Teachers"
	};
	
	private static String [] groupPrivacy = {
		"Public", "Group Members Only", "Group Admins Only"
	};
	
	int entity = -1;
	private List<String> options;
	private static final long serialVersionUID = -4093838464155355883L;
	private int option;
}
